package com.itheima.service.db.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.itheima.domain.db.*;
import com.itheima.mapper.*;
import com.itheima.service.db.TestSoulService;
import com.itheima.vo.QuestionnaireVo;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Guanxing
 * @date 2021/10/8
 */
@Service
public class TestSoulServiceImpl implements TestSoulService {
    @Autowired   //查询问卷
    private QuestionnaireMapper questionnaireMapper;

    @Autowired    //查询试题
    private TestSoulQuestionMapper testSoulQuestionMapper;

    @Autowired      //查询试题选项
    private TestSoulQuestionOptionMapper testSoulQuestionOptionMapper;

    @Autowired
    private QuestionUserLockMapper questionUserLockMapper;

    @Autowired
    private QuestionnaireResultMapper questionnaireResultMapper;

    @Autowired
    private QuestionnaireReportMapper questionnaireReportMapper;

    //查询出来  问卷
    @Override
    public List<Questionnaire> selectListQuestionnaire() {
        return questionnaireMapper.selectList(new QueryWrapper<>());
    }

    //跟据问卷id查询问题
    @Override
    public List<SoulQuestion> selectquestionList(Long id) {
        QuestionnaireVo questionnaireVo = new QuestionnaireVo();
        QueryWrapper queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("questionnaire_id",id);
        return testSoulQuestionMapper.selectList(queryWrapper);
    }

    //查询所有问卷
    @Override
    public List<Questionnaire> findQuestionnaireList() {
        List<Questionnaire> questionnaireList = questionnaireMapper.selectList(new QueryWrapper<>());

        return questionnaireList;
    }

    //根据问卷id查询问题集合
    @Override
    public List<SoulQuestion> findSoulQuestionListByQuestionnaireId(Long id) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("questionnaire_id", id);

        return testSoulQuestionMapper.selectList(queryWrapper);
    }

    //根据问题id查询选项集合
    @Override
    public List<SoulQuestionOption> findSoulQuestionOptionListByQuestionId(Long id) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("question_id", id);

        return testSoulQuestionOptionMapper.selectList(queryWrapper);
    }

    //根据问卷id查询锁状态
    @Override
    public QuestionUserLock findQuestionUserLockByQuestionnaireId(Long id) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("questionnaire_id", id);
        return questionUserLockMapper.selectOne(queryWrapper);
    }

    //根据问题id查询出选项
    @Override
    public List<SoulQuestionOption> selectQuestionOptionList(Long id) {
        QueryWrapper queryWrapper1 = new QueryWrapper();
        queryWrapper1.eq("question_id", id);

        //查询出来的关于选项的集合（封装完毕）
        return testSoulQuestionOptionMapper.selectList(queryWrapper1);
    }

    //根据 问题编号  选项编号  查询分数
    @Override
    public Integer findFenShu(String questionId, String optionId) {
        //根据  试卷id  和  选项编号  查询  选项表
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("question_id", questionId);
        queryWrapper.eq("id", optionId);
        SoulQuestionOption soulQuestionOption = testSoulQuestionOptionMapper.selectOne(queryWrapper);
        return Integer.parseInt(soulQuestionOption.getScore());
    }

    //根据分数范围返回报告id
    @Override
    public String findReportId(Integer fenshu) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.ge("scope2",fenshu);
        queryWrapper.le("scope1", fenshu);
        QuestionnaireResult questionnaireResult = questionnaireResultMapper.selectOne(queryWrapper);
        return questionnaireResult.getId().toString();
    }

    //根据报告id查询报告
    @Override
    public QuestionnaireResult findReportById(String id) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("id", id);
        queryWrapper.eq("questionnaire_id",1);
        QuestionnaireResult questionnaireResult = questionnaireResultMapper.selectOne(queryWrapper);
        return questionnaireResult;
    }

    //查询报告表中   类型相同的人的id
    @Override
    public List<Long> findUserIdByConclusion(String content) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("conclusion", content);
        List<QuestionnaireReport> list = questionnaireReportMapper.selectList(queryWrapper);
        List<Long> userIdList = new ArrayList<>();
        if (CollectionUtil.isNotEmpty(list)) {
            for (QuestionnaireReport questionnaireReport : list) {
                Long userId = questionnaireReport.getUserId();
                userIdList.add(userId);
            }
        }

        return userIdList;
    }

    //存储历史报告
    @Override
    public void saveLastReport(QuestionnaireReport questionnaireReport) {

        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("user_id", questionnaireReport.getUserId());
        queryWrapper.eq("level", questionnaireReport.getLevel());
        QuestionnaireReport questionnaireReport1 = questionnaireReportMapper.selectOne(queryWrapper);
        if (questionnaireReport1 != null) {
            questionnaireReportMapper.update(questionnaireReport,queryWrapper);
        } else {
            questionnaireReportMapper.insert(questionnaireReport);
        }
    }

    //根据答案的id查询   是那套试卷
    @Override
    public Long findQuestionnaireId(String questionId) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("id", questionId);
        SoulQuestion soulQuestion = testSoulQuestionMapper.selectOne(queryWrapper);
        return soulQuestion.getQuestionnaireId();
    }

    //查询试卷等级
    @Override
    public String findQuestionnaireLevel(Long questionnaireId) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("id", questionnaireId);
        Questionnaire questionnaire = questionnaireMapper.selectOne(queryWrapper);
        return questionnaire.getLevel().toString();
    }

    //保存或修改问卷
    @Override
    public void saveOrUpdateQuestionnaire(Questionnaire questionnaire) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("id", questionnaire.getId());
        Questionnaire dbQuestionnaire = questionnaireMapper.selectOne(queryWrapper);

        if (dbQuestionnaire == null) {
            questionnaireMapper.insert(questionnaire);
        } else {
            questionnaireMapper.updateById(questionnaire);
        }
    }

    //保存或修改问题
    @Override
    public void saveOrUpdateSoulQuestion(SoulQuestion question) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("questionnaire_id", question.getQuestionnaireId());
        SoulQuestion soulQuestion = testSoulQuestionMapper.selectOne(queryWrapper);

        if (soulQuestion == null) {
            testSoulQuestionMapper.insert(question);
        } else {
            testSoulQuestionMapper.updateById(question);
        }
    }

    //保存或修改选项
    @Override
    public void saveOrUpdateSoulQuestionOption(SoulQuestionOption option) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("question_id", option.getQuestionId());
        SoulQuestionOption soulQuestionOption = testSoulQuestionOptionMapper.selectOne(queryWrapper);

        if (soulQuestionOption == null) {
            testSoulQuestionOptionMapper.insert(option);
        } else {
            testSoulQuestionOptionMapper.updateById(option);
        }
    }

    //根据问卷id查询问卷对象
    @Override
    public Questionnaire findQuestionnaireById(Long id) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("id", id);

        return questionnaireMapper.selectOne(queryWrapper);
    }
    //查询最新的报告的conclusion
    @Override
    public String findLastReportId(Long id,String level) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("user_id", id);
        queryWrapper.eq("level", level);
        QuestionnaireReport questionnaireReport = questionnaireReportMapper.selectOne(queryWrapper);
        if (questionnaireReport != null) {
            return questionnaireReport.getConclusion();
        }
        return null;
    }
    //根据报告的内容查询报告的id
    @Override
    public String findLastReportIdBy(String conclusion) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("content", conclusion);
        queryWrapper.eq("questionnaire_id", 1);
        QuestionnaireResult questionnaireResult = questionnaireResultMapper.selectOne(queryWrapper);
        return questionnaireResult.getId().toString();
    }

    //保存所有题目
    @Override
    public void saveSoulQuestion(SoulQuestion soulQuestion) {
        if (soulQuestion != null) {
            testSoulQuestionMapper.insert(soulQuestion);
        }
    }

    @Override
    public void saveSoulQuestionOption(SoulQuestionOption soulQuestionOption) {
        if (soulQuestionOption != null) {
            testSoulQuestionOptionMapper.insert(soulQuestionOption);
        }
    }

    //保存问卷
    @Override
    public Long saveQuestionnaire(Questionnaire questionnaire) {
        questionnaireMapper.insert(questionnaire);

        Questionnaire questionnaire1 = questionnaireMapper.selectById(questionnaire);

        return questionnaire1.getId();

    }

    @Override
    public void saveQuestionUserLock(QuestionUserLock questionUserLock) {
        if (questionUserLock != null) {
            questionUserLockMapper.insert(questionUserLock);
        }
    }

    //删除选项
    @Override
    public void deleteOptionById(Long id) {
        testSoulQuestionOptionMapper.deleteById(id);
    }

    //删除问题
    @Override
    public void deleteQuestion(Long questionId) {
        testSoulQuestionMapper.deleteById(questionId);
    }

    //删除问卷
    @Override
    public void deleteByQuestionnaireId(Long questionnaireId) {
        questionnaireMapper.deleteById(questionnaireId);
    }
}
